package com.we.flink.dcapiuserlabel.tag.telsale;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.dcapiuserlabel.table.TableTaskUser;

public class TagTaskUser {
    public long uid;
    public int connect_id;
    public int update_time;

    public TagTaskUser(long uid, int connect_id, int update_time) {
        this.uid = uid;
        this.connect_id = connect_id;
        this.update_time = update_time;
    }

    public static TagTaskUser init(int connect_id) {
        return new TagTaskUser(0L, connect_id, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableTaskUser.QUERYSQL);
        DataStream<Tuple2<Boolean, TableTaskUser>> inputStream =
                tableEnv.toRetractStream(tableRes, TableTaskUser.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableTaskUser>>() {
                                    @Override
                                    public boolean filter(Tuple2<Boolean, TableTaskUser> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .uid("tagTaskUser-filter")
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableTaskUser>, Integer>() {
                                    @Override
                                    public Integer getKey(Tuple2<Boolean, TableTaskUser> value)
                                            throws Exception {
                                        return value.f1.getConnect_id();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<Tuple2<Boolean, TableTaskUser>, String>() {
                                    ValueState<TagTaskUser> taskUserVstate;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        taskUserVstate =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagTaskUser>(
                                                                        "TaskUserVstate",
                                                                        TagTaskUser.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableTaskUser> value,
                                            Collector<String> out)
                                            throws Exception {
                                        int connect_id = value.f1.getConnect_id();
                                        int uid = value.f1.getUid();
                                        int update_time =
                                                (int) (value.f1.getUpdate_time().getTime() / 1000);

                                        TagTaskUser tmpState = taskUserVstate.value();
                                        TagTaskUser curState =
                                                tmpState == null
                                                        ? TagTaskUser.init(connect_id)
                                                        : tmpState;

                                        if (update_time > curState.update_time) {
                                            if (curState.uid != uid) {
                                                JSONObject jsonObject = new JSONObject();
                                                jsonObject.put("connect_id", connect_id);
                                                jsonObject.put("uid", uid);
                                                out.collect(jsonObject.toString());

                                                curState.uid = uid;
                                                curState.update_time = update_time;
                                                taskUserVstate.update(curState);
                                            }
                                        }
                                    }
                                });

        return out;
    }
}
